package com.qubuyer.business.good.activity

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.qubuyer.R
import com.qubuyer.bean.event.SOSelectAddressEvent
import com.qubuyer.bean.event.SOSelectInvoiceEvent
import com.qubuyer.business.auth.view.LoginActivity
import com.qubuyer.business.good.adapter.SOGoodListAdapter
import com.qubuyer.business.good.viewmodel.SOViewModel
import com.qubuyer.business.mine.activity.AddressListActivity
import com.qubuyer.business.order.PayOrderViewModel
import com.qubuyer.business.order.activity.OrderNowPayActivity
import com.qubuyer.business.order.activity.OrderNowPayFragment
import com.qubuyer.core.app.MVVMFragment
import com.qubuyer.core.ext.format
import com.qubuyer.customview.DecorationLLM
import com.qubuyer.databinding.FragmentSoBinding
import com.qubuyer.utils.DialogUtil
import com.qubuyer.utils.NavigationUtil
import com.qubuyer.utils.SessionUtil
import org.greenrobot.eventbus.Subscribe
import java.util.*

/**
 * @author Susong
 * @date 创建时间:2019/1/24
 * @description 提交订单Activity
 * & @version
 */
class SOFragment : MVVMFragment<FragmentSoBinding, SOViewModel>() {

    //订单号
    private var mOrderPayId: String? = null
    private lateinit var mAdapter: SOGoodListAdapter
    private val payOrderViewModel: PayOrderViewModel by injectVMByActivity()


    override fun getTitle(): String {
        return "确认订单"
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mAdapter = SOGoodListAdapter()
        viewDataBinding.rvGoodList.addItemDecoration(DecorationLLM(mContext, DecorationLLM.VERTICAL_LIST, R.drawable.shape_recyclerview_divider, ConvertUtils.dp2px(15f)))
        viewDataBinding.rvGoodList.layoutManager = LinearLayoutManager(mContext)
        viewDataBinding.rvGoodList.adapter = mAdapter
        viewModel.orderInfo.observe(viewLifecycleOwner, {
            mAdapter.setDataList(it.cartList)
            if (null != it.cartPriceInfo) {
                viewDataBinding.tvGoodPrice.text = "¥${(it.cartPriceInfo?.total_fee ?: 0.0).toDouble().format(2)}"
                viewDataBinding.tvGoodFreight.text = "+¥0.00"
                viewDataBinding.tvGoodDiscounts.text = "-¥0.00"
                viewDataBinding.tvMoney.text = it.cartPriceInfo?.total_fee.toString()
            }
        })
        viewModel.resultInfo.observe(viewLifecycleOwner, {
            mOrderPayId = it.order_id
            payOrderViewModel.mSOEntity = it
            mContext.swapContentFragment(OrderNowPayFragment())
        })
        viewModel.priceInfo.observe(viewLifecycleOwner, {
            viewDataBinding.tvGoodPrice.text = "¥${it.goods_price.toDouble().format(2)}"
            viewDataBinding.tvGoodFreight.text = "+¥${it.shipping_price.toDouble().format(2)}"
            viewDataBinding.tvGoodDiscounts.text = "-¥${it.order_cut_price.toDouble().format(2)}"
            viewDataBinding.tvMoney.text = it.order_amount.toDouble().format(2)
            viewDataBinding.tvMsg1.isVisible = !it.msg1.isNullOrEmpty()
            viewDataBinding.tvMsg2.isVisible = !it.msg2.isNullOrEmpty()
            viewDataBinding.tvMsg1.text = it.msg1
            viewDataBinding.tvMsg2.text = it.msg2
        })
        viewModel.addressInfo.observe(viewLifecycleOwner, {
            if (it != null) {
                viewDataBinding.tvAddAddress.isVisible = false
                viewDataBinding.llAddressINfo.isVisible = true
                viewDataBinding.tvName.text = it.consignee
                viewDataBinding.tvPhone.text = it.mobile
                viewDataBinding.tvAddress.text = it.address_area + it.address
                viewModel.calcOrderPrice(it.address_id ?: "")
            } else {
                viewDataBinding.llAddressINfo.isVisible = false
                viewDataBinding.tvAddAddress.isVisible = true
            }
        })
        viewModel.invoiceInfo.observe(viewLifecycleOwner, {
            viewDataBinding.tvInvoiceMode.text = if (it.isUse == 1) "电子发票-${if (it.type == 1) "个人" else "单位"}" else "不开发票"
        })
        viewModel.getSOInfo()
    }

    fun onClick(v: View) {
        when (v.id) {
            R.id.tvAddAddress, R.id.llAddressINfo -> NavigationUtil.overlay(mContext, AddressListActivity::class.java, true)
            R.id.rl_invoice -> NavigationUtil.overlay(mContext, GoodInvoiceActivity::class.java, viewModel.invoiceInfo.value)
            R.id.tv_submit_order -> {
                if (!SessionUtil.getInstance().isLogined) {
                    DialogUtil.getConfirmDialog(mContext, "提示", "需要登录后，才能继续以下操作，是否现在登录？", "登录", "取消", false, { dialog, which -> NavigationUtil.overlay(mContext, LoginActivity::class.java) }) { dialog, which -> dialog.dismiss() }.show()
                    return
                }
                if (null == viewModel.addressInfo.value) {
                    toast("请选择收货地址")
                    return
                }
                viewModel.submitOrder(viewDataBinding.etLeaveMessage.text.toString())
            }
        }
    }

    override fun onDataBinding(dataBinding: FragmentSoBinding) {
        dataBinding.host = this
    }

    override val viewModel: SOViewModel by injectVMByActivity()
    override val layoutId: Int
        get() = R.layout.fragment_so

    @Subscribe
    fun onEvent(event: SOSelectAddressEvent) {
        viewModel.addressInfo.value = event.entity
    }

    @Subscribe
    fun onEvent(event: SOSelectInvoiceEvent) {
        viewModel.invoiceInfo.value = event.entity
    }
}